package rmiserver;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import core.Controller;

import messages.LoginMessage;
import messages.RegisterMessage;
import user.User;

public class LogonServiceImpl extends UnicastRemoteObject implements LogonService {

	public LogonServiceImpl() throws RemoteException {
		super();
	}

	public ServerRemote login(LoginMessage message, ClientRemote client) throws RemoteException {
		System.err.println("ola");
		
		RMIClientCommunicator communicator = new RMIClientCommunicator(client);
		User user = Controller.getInstance().authenticate(message, communicator);
		
		if (user != null) {
			return new RMIClientHandler(user);
		}
		
		return null;
	}


	public ServerRemote register(RegisterMessage message, ClientRemote client) throws RemoteException {
		RMIClientCommunicator communicator = new RMIClientCommunicator(client);
		User user = Controller.getInstance().register(message, communicator);
		
		if (user != null) {
			return new RMIClientHandler(user);
		}
		
		return null;
	}
	
	
	private static final long serialVersionUID = 1L;
}
